package com.myk.game.gobangchess.ai.startzone;

import java.util.Arrays;
import java.util.Random;

public class MutationAlgorithm {
    public static int[][] mutation(int[][] population, double mutationRate, int n, int m, int[][] baseBoard, int color) {
        int[][] newPop = new int[population.length][n];
        Random rand = new Random();

        for (int i = 0; i < population.length; i++) {
            if (rand.nextDouble() < mutationRate) {
                int point = rand.nextInt(n);
                newPop[i] = population[i].clone();
                newPop[i][point] = 1 - newPop[i][point];
                newPop[i] = GeneFixer.fixGenes(newPop[i], m, baseBoard, color);
            } else {
                newPop[i] = population[i].clone();
            }
        }

        return newPop;
    }

    public static void main(String[] args) {
        double mutationRate = 0.35;
        int n = 46;
        int m = 14;
        int[][] base_board = {
            {3, 3, 3, 1, 2, 3, 3, 3, 3},
            {3, 3, 0, 0, 0, 0, 0, 0, 3},
            {3, 0, 0, 0, 0, 0, 0, 0, 3},
            {1, 0, 0, 0, 0, 0, 0, 0, 3},
            {2, 0, 0, 0, 2, 0, 0, 0, 2},
            {3, 0, 0, 0, 0, 0, 0, 0, -1},
            {3, 0, 0, 0, 0, 0, 0, 0, 3},
            {3, 0, 0, 0, 0, 0, 0, 3, 3},
            {3, 3, 3, 3, 2, -1, 3, 3, 3}
        };
        int color = 1;
        int[][] selected = {
            {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0},
            {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}};
    

        int[][] mutatedPopulation = mutation(selected, mutationRate, n, m, base_board, color);

        System.out.println("\nMutated Population:");
        for (int[] individual : mutatedPopulation) {
            System.out.println(Arrays.toString(individual));
        }
    }
}
